import 'package:flutter/material.dart';

import 'pages/login/index.dart';
import 'pages/notice/index.dart';
import 'pages/profile/index.dart';
import 'pages/tabs_page.dart';
import 'utils/TokenManager.dart';

void main() {
  runApp(MaterialApp(
    routes: {
      '/': (context) => const TabsPage(),
      '/noticeDetail': (context) => const NoticeDetail(),
      '/login': (context) => const LoginPage(),
      // '/profile': (context) => const ProfilePage(),
    },
    initialRoute: '/',
    //!在跳转时如果routes中没有匹配的页面，代码会执行到onGendrateRoute中,同样就可以理解成routes是白名单
    onGenerateRoute: (settings) {
      final token = tokenManager.getToken();
      if (token.isEmpty) {
        //如果Token为空直接拦截到登录页
        return MaterialPageRoute(builder: (context) => const LoginPage());
      } else {
        if (settings.name == '/profile') {
          return MaterialPageRoute(builder: (context) => const ProfilePage());
        }
      }
      return null;
    },
  ));
}
